package com.blueheart.music.service.impl;

import com.blueheart.music.exception.BaseException;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.mapper.CollectMapper;
import com.blueheart.music.model.pojo.Collect;
import com.blueheart.music.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gali
 * @date 2022/4/10 17:10
 */
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    /**
     * 增加收藏
     *
     * @param collect
     * @return
     */
    @Override
    public boolean addCollect(Collect collect) {
        if (collect.getType()==0){   //收藏的为歌曲
            Collect collect1 = collectMapper.checkSongInUser(collect.getUserId(), collect.getSongId());
            if (collect1==null){
               return collectMapper.insertSelective(collect)>0;
            }
            throw  new BaseException(BaseExceptionEnum.SONG_EXIST);
        }
        Collect collect1 = collectMapper.checkSongListInUser(collect.getUserId(), collect.getSongListId());
        if (collect1==null){
            return collectMapper.insertSelective(collect)>0;
        }
        throw  new BaseException(BaseExceptionEnum.SONGLIST_EXIST);
    }

    /**
     * 删除收藏
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeCollect(Integer id) {
        return collectMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 修改收藏
     *
     * @param collect
     * @return
     */
    @Override
    public boolean reviseCollect(Collect collect) {
        if (collect.getType()==0){   //收藏的为歌曲
            Collect collect1 = collectMapper.checkSongInUser(collect.getUserId(), collect.getSongId());
            if (collect1==null){
                return collectMapper.updateByPrimaryKeySelective(collect)>0;
            }
            throw  new BaseException(BaseExceptionEnum.SONG_EXIST);
        }
        Collect collect1 = collectMapper.checkSongListInUser(collect.getUserId(), collect.getSongListId());
        if (collect1==null){
            return collectMapper.updateByPrimaryKeySelective(collect)>0;
        }
        throw  new BaseException(BaseExceptionEnum.SONGLIST_EXIST);
    }

    /**
     * 查询全部收藏
     *
     * @return
     */
    @Override
    public List<Collect> findAll() {
        return collectMapper.selectAll();
    }

    /**
     * id查询收藏
     *
     * @param id
     * @return
     */
    @Override
    public Collect findById(Integer id) {
        return collectMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询个人收藏
     *
     * @param userId
     * @return
     */
    @Override
    public List<Collect> findByUserId(Integer userId) {
        return collectMapper.selectByUserId(userId);
    }

    /**
     * 查询是否收藏了该歌曲
     *
     * @param userId
     * @param songId
     * @return
     */
    @Override
    public boolean checkSongInUser(Integer userId, Integer songId) {
        return collectMapper.checkSongInUser(userId,songId)!=null;
    }

    /**
     * 检查是否收藏了该歌单
     *
     * @param userId
     * @param songListId
     * @return
     */
    @Override
    public boolean checkSongListInUser(Integer userId, Integer songListId) {
        return collectMapper.checkSongListInUser(userId, songListId)!=null;
    }

    /**
     * 查询主键
     *
     * @param userId
     * @param songId
     * @return
     */
    @Override
    public Collect findCollectId(Integer userId, Integer songId) {
        return collectMapper.checkSongInUser(userId, songId);
    }
}
